package GUI;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import Data.*;

/**
 * Cau truc ClassFrame
<ClassFrame>
	<classNamePanel>
		<classnameTextField />
	</classnamePanel>
	
	<splitPane>
		<attributesScrollPane>
			<fullAttributesPanel>
				<attributesPanel>
					<AttributeAndMethod>
						<nameTextField />
						<deleteButton />
					</AttributeAndMethod>
					....................
					<AttributeAndMethod>
						<nameTextField />
						<deleteButton />
					</AttributeAndMethod>
				</attributesPanel>
				<addAttributeButton />
			</fullAttributesPanel>
		</attributesScrollPane>
		
		<methodsScrollPane>
			<fullMethodsPanel>
				<methodsPanel>
					<AttributeAndMethod>
						<nameTextField />
						<deleteButton />
					</AttributeAndMethod>
					....................
					<AttributeAndMethod>
						<nameTextField />
						<deleteButton />
					</AttributeAndMethod>				
				</methodsPanel>
				<addMethodButton />
			</fullMethodsPanel>
		</methodsScrollPane>
	</splitPane>
	
</ClassFrame>
*/

public class ClassFrame extends JInternalFrame implements ActionListener {
	private final Color BACKGROUND = new Color(250, 250, 250);
	
	Log log;
	Classes classes;
	
	Data.Class classData;
	
	ClassFrame me = this;
	private MainFrame mainFrame;
	
	private JPanel classNamePanel;
	private JTextField classNameTextField;
	private JPanel attributesPanel;
	private JButton addAttributeButton;
	private JButton deleteAllAttributeButton;
	private JPanel methodsPanel;
	private JButton addMethodButton;
	private JButton deleteAllMethodButton;
	
	public ClassFrame(MainFrame m) {
		log = Log.getInstance();
		classes = Classes.getInstance();
		
		Data.Class c = new Data.Class();
		classData = c;
		classes.addClass(c);
		
		mainFrame = m;
		
		super.setResizable(true);
		ResourceBundle lang = Options.getLanguage();
		setLayout(new BorderLayout(2, 1));
		
		classNamePanel = new JPanel();
		add(classNamePanel, BorderLayout.NORTH);
		classNameTextField = new JTextField("Class name");
		classNamePanel.add(classNameTextField);
		
		attributesPanel = new JPanel();
		attributesPanel.setBackground(BACKGROUND);
		JPanel fullAttributesPanel = new JPanel(new BorderLayout());
		fullAttributesPanel.add(attributesPanel);

		addAttributeButton = new JButton(lang.getString("ClassFrame.addAttributeButton.text"));
		addAttributeButton.addActionListener(this);
		addAttributeButton.setBorder(null);
		addAttributeButton.setBackground(new Color(200, 200, 200));
		fullAttributesPanel.add(addAttributeButton, BorderLayout.SOUTH);
		JScrollPane attributesScrollPane= new JScrollPane(fullAttributesPanel, JScrollPane.VERTICAL_SCROLLBAR_NEVER, 
																	JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		
		deleteAllAttributeButton = new JButton(lang.getString("ClassFrame.deleteAllAttributeButton.text"));
		deleteAllAttributeButton.addActionListener(this);
		deleteAllAttributeButton.setBorder(null);
		deleteAllAttributeButton.setBackground(new Color(200, 200, 200));
		//fullAttributesPanel.add(deleteAllAttributeButton, BorderLayout.SOUTH);
		
		attributesPanel.setLayout(new GridLayout(0, 1));
	
		
		
		
		methodsPanel = new JPanel();
		methodsPanel.setBackground(BACKGROUND);
		JPanel fullMethodsPanel = new JPanel(new BorderLayout());
		fullMethodsPanel.add(methodsPanel);

		addMethodButton = new JButton(lang.getString("ClassFrame.addMethodButton.text"));
		addMethodButton.addActionListener(this);
		addMethodButton.setBorder(null);
		addMethodButton.setBackground(new Color(200, 200, 200));
		fullMethodsPanel.add(addMethodButton, BorderLayout.SOUTH);
		JScrollPane methodsScrollPane= new JScrollPane(fullMethodsPanel, JScrollPane.VERTICAL_SCROLLBAR_NEVER,
																	JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		
		
		
		GridLayout t = new GridLayout(0, 1);
		
		methodsPanel.setLayout(t);
		//t.setVgap(-5);
		
		
		
		
		JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, attributesScrollPane, methodsScrollPane);
		add(splitPane);
		splitPane.setResizeWeight(0.5);

		setBackground(BACKGROUND);
		setSize(200, 250);
		this.setBorder(BorderFactory.createLineBorder(Color.black));
		setVisible(true);

		attributesPanel.setBorder(null);
		fullAttributesPanel.setBorder(null);
		attributesScrollPane.setBorder(null);
		methodsPanel.setBorder(null);
		fullMethodsPanel.setBorder(null);
		methodsScrollPane.setBorder(null);
		splitPane.setBorder(null);
		
		this.addInternalFrameListener(new InternalFrameHandler());
	}
	

	public ClassFrame(ClassWithShape cwt, MainFrame m) {
		this(m);
		classData = new Data.Class(cwt.GetClass());
		LinkedList<String> attributes = cwt.GetClass().getAttributes();
		LinkedList<String> methods = cwt.GetClass().getMethods();
		
		int size = attributes.size();
		for (int i = 0; i < size; i++) {
			addAttribute(attributes.get(i));
		}
		
		size = methods.size();
		for (int i = 0; i < size; i++) {
			addMethod(methods.get(i));
		}
	}
	
	public ClassFrame(ClassFrame cf) {
		this(cf.mainFrame);
		classData = new Data.Class(cf.classData);
		Component tmp[] = cf.attributesPanel.getComponents();
		int size = tmp.length;
		for (int i = 0; i < size; i++) {
			attributesPanel.add(new Attribute((Attribute)tmp[i], this));
		}
		tmp = cf.methodsPanel.getComponents();
		size = tmp.length;
		for (int i = 0; i < size; i++) {
			methodsPanel.add(new Method((Method)tmp[i], this));
		}
	}
	
	public void copy(ClassFrame cf) {
		mainFrame = cf.mainFrame;
		attributesPanel.removeAll();
		methodsPanel.removeAll();
		
		classData = new Data.Class(cf.classData);
		Component tmp[] = cf.attributesPanel.getComponents();
		int size = tmp.length;
		for (int i = 0; i < size; i++) {
			attributesPanel.add(new Attribute((Attribute)tmp[i], this));
		}
		tmp = cf.methodsPanel.getComponents();
		size = tmp.length;
		for (int i = 0; i < size; i++) {
			methodsPanel.add(new Method((Method)tmp[i], this));
		}
		this.updateUI();
	}
	
	public void actionPerformed(ActionEvent event) {
		if (event.getSource() == addAttributeButton) {
			addAttribute(null);
		} else if (event.getSource() == addMethodButton) {
			addMethod(null);
		}
	}
	
	public void addAttribute(String text) {
		AttributeMethod tmp = new Attribute(text, this);
		attributesPanel.add(tmp);
		this.updateUI();
		log.push(new ClassWithShape(classData, this));
		addAttributeButton.scrollRectToVisible(attributesPanel.getVisibleRect());
		tmp.getNameReference().requestFocus();
	}
	
	public void addMethod(String text) {
		AttributeMethod tmp = new Method(text, this);
		methodsPanel.add(tmp);
		this.updateUI();
		log.push(new ClassWithShape(classData, this));
		addMethodButton.scrollRectToVisible(methodsPanel.getVisibleRect());
		tmp.getNameReference().requestFocus();
	}
	
	public void updateClass(ClassWithShape cwt) {
		attributesPanel.removeAll();
		methodsPanel.removeAll();
		
		LinkedList<String> attributes = cwt.GetClass().getAttributes();
		LinkedList<String> methods = cwt.GetClass().getMethods();
		
		int size = attributes.size();
		for (int i = 0; i < size; i++) {
			addAttribute(attributes.get(i));
		}
		
		size = methods.size();
		for (int i = 0; i < size; i++) {
			addMethod(methods.get(i));
		}
	}
	
	public void set() {
		
	}
	
	private class InternalFrameHandler extends InternalFrameAdapter {
		public void internalFrameActivated(InternalFrameEvent event) {
			//mainFrame.activedClassFrame = me;
			me.setBorder(BorderFactory.createLineBorder(Color.red));
		}
		public void internalFrameDeactivated(InternalFrameEvent event) {
			//mainFrame.activedClassFrame = me;
			me.setBorder(BorderFactory.createLineBorder(Color.blue));
			if (mainFrame.lastActivedClassFrame != null) mainFrame.lastActivedClassFrame.setBorder(BorderFactory.createLineBorder(Color.black));
			mainFrame.lastActivedClassFrame = me;
		}
	}
}
